//
//  AppDelegate.m
//  wzry
//
//  Created by 酷安 @啥也不会啊啊啊啊啊啊  on 2022/10/10.
//

#import "AppDelegate.h"
#import "HomeVC.h"
#import "MoreViewController.h"


#import "QMUIConfigurationTemplateGrapefruit.h"
#import "QMUIConfigurationTemplateGrass.h"
#import "QMUIConfigurationTemplatePinkRose.h"
#import "QMUIConfigurationTemplateDark.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 1. 先注册主题监听，在回调里将主题持久化存储，避免启动过程中主题发生变化时读取到错误的值
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleThemeDidChangeNotification:) name:QMUIThemeDidChangeNotification object:nil];
    
    
    // 2. 然后设置主题的生成器
    QMUIThemeManagerCenter.defaultThemeManager.themeGenerator = ^__kindof NSObject * _Nonnull(NSString * _Nonnull identifier) {
        if ([identifier isEqualToString:QDThemeIdentifierDefault]) return QMUIConfigurationTemplateDark.new;
        if ([identifier isEqualToString:QDThemeIdentifierDark]) return QMUIConfigurationTemplateDark.new;
        
        NSLog(@"%@ 主题未能匹配",identifier);
        return QMUIConfigurationTemplate.new;
    };
    
    // 3. 再针对 iOS 13 开启自动响应系统的 Dark Mode 切换
    // 如果不需要这个功能，则不需要这一段代码
    if (@available(iOS 13.0, *)) {
        if (QMUIThemeManagerCenter.defaultThemeManager.currentThemeIdentifier) {// 做这个 if(currentThemeIdentifier) 的保护只是为了避免 QD 里的配置表没启动时，没人为 currentTheme/currentThemeIdentifier 赋值，导致后续的逻辑会 crash，业务项目里理论上不会有这种情况出现，所以可以省略这个 if，直接写下面的代码就行了
            
            QMUIThemeManagerCenter.defaultThemeManager.identifierForTrait = ^__kindof NSObject<NSCopying> * _Nonnull(UITraitCollection * _Nonnull trait) {
                // 1. 如果当前系统切换到 Dark Mode，则返回 App 在 Dark Mode 下的主题
                if (trait.userInterfaceStyle == UIUserInterfaceStyleDark) {
                    return QDThemeIdentifierDark;
                }
                
                // 2. 如果没有命中1，说明此时系统是 Light，则返回 App 在 Light 下的主题即可，这里不直接返回 Default，而是先做一些复杂判断，是因为 QMUI Demo 非深色模式的主题有好几个，而我们希望不管之前选择的是 Default、Grapefruit 还是 PinkRose，只要从 Dark 切换为非 Dark，都强制改为 Default。
                
                // 换句话说，如果业务项目只有 Light/Dark 两套主题，则按下方被注释掉的代码一样直接返回 Light 下的主题即可。
                //                return QDThemeIdentifierDefault;
                
                if ([QMUIThemeManagerCenter.defaultThemeManager.currentThemeIdentifier isEqual:QDThemeIdentifierDark]) {
                    return QDThemeIdentifierDefault;
                }
                return QMUIThemeManagerCenter.defaultThemeManager.currentThemeIdentifier;
            };
            QMUIThemeManagerCenter.defaultThemeManager.respondsSystemStyleAutomatically = YES;
        }
    }
    
//    [QMUIThemeManagerCenter.defaultThemeManager setCurrentTheme:QMUIConfigurationTemplateDark.new];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self didInitWindow];
    
    [QMUIConsole sharedInstance].canShow = [UserConfig shareInstance].isDeBug;
    [UserConfig loadLoginIps];
    return YES;
}
- (void)didInitWindow {
    self.window.rootViewController = [self generateWindowRootViewController];
    self.window.backgroundColor = UIColor.whiteColor;
    [self.window makeKeyAndVisible];
}

- (UIViewController *)generateWindowRootViewController {
    
    UITabBarController *tabBarViewController = [[UITabBarController alloc] init];
    
    // QMUIKit
    
    QMUINavigationController *homeNav = nil;
    {
        HomeVC *uikitViewController = [[HomeVC alloc] init];
        uikitViewController.hidesBottomBarWhenPushed = NO;
        homeNav = [[QMUINavigationController alloc] initWithRootViewController:uikitViewController];
//        UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle: @"首页" image:UIImageMake(@"home") tag:0];
//        homeNav.tabBarItem = tabBarItem;
    }
//    QMUINavigationController *moreav = nil;
//    {
//        MoreViewController  *vc = [[MoreViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = NO;
//        moreav = [[QMUINavigationController alloc] initWithRootViewController:vc];
//        UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle: @"更多" image:UIImageMake(@"more") tag:0];
//        moreav.tabBarItem = tabBarItem;
//    }
    
    // window root controller
//    tabBarViewController.viewControllers = @[homeNav];
//    return tabBarViewController;
    return homeNav;
}

- (void)handleThemeDidChangeNotification:(NSNotification *)notification {
    
    QMUIThemeManager *manager = notification.object;
    if (![manager.name isEqual:QMUIThemeManagerNameDefault]) return;
    
    [[NSUserDefaults standardUserDefaults] setObject:manager.currentThemeIdentifier forKey:QDSelectedThemeIdentifier];
    
    [QDThemeManager.currentTheme applyConfigurationTemplate];
    
    //    if (QMUIHelper.canUpdateAppearance) {
    //        // 主题发生变化，在这里更新全局 UI 控件的 appearance
    //        [QDCommonUI renderGlobalAppearances];
    //
    //        // 更新表情 icon 的颜色
    //        [QDUIHelper updateEmotionImages];
    //    }
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait;
}
@end
